package com.htfyun.roobofactory.factorytest;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.htfyun.roobofactory.BaseActivity;
import com.htfyun.roobofactory.R;
import com.htfyun.roobofactory.config.RooboBuildConfig;

/**
 * Created by sst on 2018/7/8.
 */

public class LightSensorTestActivity extends BaseActivity {

    private TextView txtValue;
    private TextView txtMaxMinValue;

    private Button btnPass;
    private Button btnFail;

    private SensorManager sensorManager;
    private SensorEventListener sensorEventListener;
    private Sensor lightSensor;

    private static final float LIGHT_SENSOR_MIN_LUX_CHANGED = (float) 120.0;
    private float   mLuxValueMin = 0;
    private float   mLuxValueMax = 0;

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_factorytest_light_sensor);

        mContext = this;

        txtValue = (TextView) findViewById(R.id.txtValue);
        txtMaxMinValue = (TextView) findViewById(R.id.txtMaxMiValue);

        btnPass = (Button) findViewById(R.id.btnPass);
        btnFail = (Button) findViewById(R.id.btnFail);
        btnPass.setOnClickListener(onPassOrFailClickListener);
        btnFail.setOnClickListener(onPassOrFailClickListener);

        btnPass.setVisibility(RooboBuildConfig.FT_ADD_PASS_BTN ? View.VISIBLE : View.GONE);

        sensorManager = getSystemService(SensorManager.class);
        lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

        sensorEventListener = new MySensorListener();

    }

    @Override
    protected void onResume() {
        super.onResume();

        sensorManager.registerListener(sensorEventListener, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
        mLuxValueMin = Float.MAX_VALUE;
        mLuxValueMax = Float.MIN_VALUE;

    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(sensorEventListener);
    }

    private View.OnClickListener onPassOrFailClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btnPass) {

                setResult(RESULT_OK);
                ActivityCompat.finishAfterTransition(getActivity());

            } else if (v == btnFail) {

                setResult(RESULT_CANCELED);
                ActivityCompat.finishAfterTransition(getActivity());

            }
        }
    };

    private final class MySensorListener implements SensorEventListener {

        @Override
        public void onSensorChanged(SensorEvent event) {
            //获取精度
            float accuracy = event.accuracy;
            //获取光线强度
            float lux = event.values[0];

            if( lux < mLuxValueMin ){
                mLuxValueMin = lux;
            }
            if( lux > mLuxValueMax ){
                mLuxValueMax = lux;
            }

            if( (lux > 0) && (lux < 225) ){
                if ( mLuxValueMax - mLuxValueMin >= LIGHT_SENSOR_MIN_LUX_CHANGED) {
                    //pass

                    startAutoPass(R.string.test_light_sensor_pass);
                }

                String log = getString(R.string.test_light_sensor_log_en, mLuxValueMin, mLuxValueMax, LIGHT_SENSOR_MIN_LUX_CHANGED);
                FactoryTestItemsEnum.test_light_sensor.setLog(mContext, log);
            }

            txtValue.setText(getString(R.string.test_light_sensor_value, lux));
            txtMaxMinValue.setText(getString(R.string.test_light_sensor_max_min_value, mLuxValueMax, mLuxValueMin));

        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {


        }
    }
}
